package com.itheima.dao;

import com.itheima.domain.Student;

import java.util.ArrayList;

/*
学生数据操作类
增
删
改
查
操作集合
 */
public class StudentDao implements BaseStudentDao{
    private static ArrayList<Student> list = new ArrayList<>();
   static  {
        Student student1 = new Student("0001","坤坤",26,"1996-01-02");
        Student student2 = new Student("0002","小鹿",30,"1993-01-02");
        Student student3 = new Student("0003","签签",31,"1992-01-02");
        Student student4 = new Student("0004","滔滔",31,"1991-01-02");
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
    }
    @Override
    public void addStudent(Student student) {
        //真正的完成新增动作
        //创建集合  添加元素
        list.add(student);
    }

    public ArrayList<Student> findAllStudents() {
        return list;
    }

    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        //根据索引删除
        list.remove(index);
    }

    public int getIndex(String sid) {
        int index=-1;
        //根据学号找索引
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student.getSid().equals(sid)){
                index=i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String sid, Student student) {
        int index = getIndex(sid);
        list.set(index,student);
    }
}
